package com.daatak.user.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.daatak.user.domain.UserAddress;
import com.daatak.user.domain.UserObject;

@Repository
@Transactional
public class UserDao implements UserDaoI {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	
	public void setSessionFactory(SessionFactory sessionFactory) 
	{
		//hibernateTemplate = new HibernateTemplate(sessionFactory);
		this.sessionFactory = sessionFactory;
	}
	
	public Session getSession() 
	{
		//hibernateTemplate = new HibernateTemplate(sessionFactory);
		return this.sessionFactory.getCurrentSession();
	}
	
	public List<UserObject> list()
	{
		//return null;//hibernateTemplate.find("from UserView");
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery("from UserObject").list();
		
	}
	
	public void create(UserObject userObject)
	{
		
		getSession().saveOrUpdate(userObject);
		
		
	}
	
	public UserObject findById(Integer Id)
	{
		
		Session session = sessionFactory.getCurrentSession();
		return (UserObject)session.get(UserObject.class,Id);
		
	}
	
	
	public UserObject loadUserByUsername(String arg0)
	{
		// TODO Auto-generated method stub
		/*List<UserObject> userList=hibernateTemplate.find("from UserObject where userName=?",arg0);
		if(userList.size()>0)
			return userList.get(0);
		else*/
			return null;
		
		
	}
	
	public void delete(UserObject  userObject)
	{
		//hibernateTemplate.delete(userObject);
	}
	
	public List<UserAddress> getUserAddress(Integer userId)
	{
		//UserObject user = userDao.getUserAddress(userId);
		Session session = sessionFactory.getCurrentSession();
		return session.createQuery("from UserAddress").list();
		
	}
	
}
